

<?php	
require "../libraries/config.php";
require "../libraries/dbclass2.php";
require "../libraries/function.php";
 ?>
<?php

function get_result($link){
					//	echo $link;
						
						
						if(strpos($link,"truyentranh8")!==false){
						return 'fail';
						}
						if(strpos($link,"vnsharing")!==false){
						return 'fail';
						}
						if(strpos($link,"anhso")!==false){
						return 'fail';
						}
						if(strpos($link,"upanh")!==false){
						return 'fail';
						}
						$link=str_replace(" ","%20",$link);
						
						if(strpos($link,"blogspot.com")!==false){
							$pos=substr(strrchr($link,'/'),1);
						
							$link=str_replace($pos,"1.jpg",$link);
							
						}
						
						$surl="url=".urlencode($link);
						$url = 'http://anime24h.vn/picasa/index4.php?c='.date('U');
						$resultContent = mycurl($surl,$url);
						$result = urldecode($resultContent);
						return $result;
}


$db=new db;
$db->connect();
$db->query("select id from chapterk2 where check_link=0 and check_error=0 and op=1 order by id asc limit 0,30000");
//$db->query("select id from chapterk2 where id=16821  order by id asc limit 0,50 ");
$dt=$db->fetchAll();
//print_r($dt);
foreach($dt as $row){
	
	//GET CHAPTERK CONTENT TRONG CHAPTERK_CONTENT----------
	$db->query("select * from chapterk_content where chap_id=".$row['id']);
	$r=$db->fetchOne();
	//KIEM TRA XEM CO LINK TRONG CHAPTERK3_CONTENT KHONG

		
	if($r['chap_page'] ==0){
		
			echo $row['id']." fail roi nha,ko co page nao \n";
			$sql="update chapterk2 set check_error=1,check_link=1 where id=".$row['id'];
			$sql2="update chapterk_content set check_error=1,check_link=1 where chap_id=".$row['id'];
			echo $sql;
			echo"\n";
			echo $sql2;
			$db->nonquery($sql);
			echo mysql_error();
			$db->nonquery($sql2);
			echo mysql_error();
			
			
					
		
	}else{	
				echo $row['id'];
					$db->query("select * from chapterk3_content where chap_id=".$row['id']);
						
					if($db->num_rows()>0){ 
					//Start co data trong chapterk3_content-------
						$r2=$db->fetchOne();
						$links=unserialize($r2['picasa']);
						
						if($r['chap_page']>=3){
							$link=$links['2'];
						}else
						if($r['chap_page']==2){
							$link=$links['1'];
						}
						else
						if($r['chap_page']==1){
							$link=$links['0'];
						}
						
						$result=get_result($link);
						
						
									if($result=='fail'){
										
										//get link trong table chapterk_content va kiem tra
										$links=unserialize($r['chap_content']);
											
										if($r['chap_page']>=3){
											$link=$links['2'];
										}else
										if($r['chap_page']==2){
											$link=$links['1'];
										}
										else
										if($r['chap_page']==1){
											$link=$links['0'];
										}
										
										$result=get_result($link);
										
										if($result=='fail'){
										
												echo $row['id']." fail chapterk_content va chapterk3_content ,".$link.'\n';
												$sql="update chapterk2 set check_error=1,check_link=1 where id=".$row['id'];
												$sql2="update chapterk_content set check_error=1,check_link=1,check_picasa=1 where chap_id=".$row['id'];
										}else{
												echo $row['id']." okay chapterk_content va   fail    chapterk3_content ,".$link.'\n';
												$sql="update chapterk2 set check_error=0,check_link=1 where id=".$row['id'];
												$sql2="update chapterk_content set check_error=0,check_link=1,check_picasa=1 where chap_id=".$row['id'];
										}
										//File fail
									
										$db->nonquery($sql);
										echo mysql_error();
										$db->nonquery($sql2);
										echo mysql_error();
										//End result ==fail
									}
									
									if($result!='fail'){
										//echo "file oke ".$link;
										echo $row['id']." okay ,".$link.'\n';
										$sql="update chapterk2 set check_link=1,check_error=0 where id=".$row['id'];
										$sql2="update chapterk_content set check_link=1, check_error=0,check_picasa=0 where chap_id=".$row['id'];
										$db->nonquery($sql);
										$db->nonquery($sql2);
										echo mysql_error();
										//echo $sql;
										//End result !=fail
									}
						
						
						
					//End cua co du lieu trong chapterk3_content	
					}else{
					//Check link trong chapterk_content-------
										$links=unserialize($r['chap_content']);
											
										if($r['chap_page']>=3){
											$link=$links['2'];
										}else
										if($r['chap_page']==2){
											$link=$links['1'];
										}
										else
										if($r['chap_page']==1){
											$link=$links['0'];
										}
										
										$result=get_result($link);
										
										if($result=='fail'){
										
												echo $row['id']." fail chapterk_content  ,".$link.'\n';
												$sql="update chapterk2 set check_error=1,check_link=1 where id=".$row['id'];
												$sql2="update chapterk_content set check_error=1,check_link=1,check_picasa=0 where chap_id=".$row['id'];
										}else{
												echo $row['id']." okay chapterk_content ,".$link.'\n';
												$sql="update chapterk2 set check_error=0,check_link=1 where id=".$row['id'];
												$sql2="update chapterk_content set check_error=0,check_link=1,check_picasa=0 where chap_id=".$row['id'];
										}
										//File fail
									
										$db->nonquery($sql);
										echo mysql_error();
										$db->nonquery($sql2);
										echo mysql_error();
										//End result ==fail
					//End of check_link trong chapterk_content----	
					}
			//Check case co link trong chapterk3_content di
			
			
			
			
				
			
			
	}
	
}
 ?>
<?php
//Delete nhung chap thieu khong co manga---
/*
$db =new db;
$db->connect();
$db->query("select id,manga from chapterk2 order by manga asc");
$dt=$db->fetchAll();
$j=0;
foreach($dt as $row){
  $sql="select id from anime where id=".$row['manga'];
  $db->query($sql);
  if($db->num_rows()==0){
  	echo $j++." : ".$row['id']." co manga id ".$row['manga']." khong ton  tai <Br/>";
	$db->nonquery("delete from chapterk2 where id=".$row['id']);
	$db->nonquery("delete from chapterk_content where chap_id=".$row['id']);
	$db->nonquery("delete from chapterk3_content where chap_id=".$row['id']);
  }
}
*/
?>
      
<?php
/*
//Clear chap thua trong chapterk_content
$db=new db;
$db->connect();
$db->query("select chap_id from chapterk3_content");
$dt=$db->fetchAll();
$j=0;
foreach($dt as $row){
	$db->query("select id from chapterk2 where id=".$row['chap_id']." and op=1");
	if($db->num_rows()>0){
		echo "Khong thua";
	}else{
		echo $j++.":".$row['chap_id']." Thua <Br/>";
		$db->nonquery("delete from chapterk_content where chap_id=".$row['chap_id']);
		$db->nonquery("delete from chapterk3_content where chap_id=".$row['chap_id']);
		echo mysql_error();
	}
}
$db->close();
*/
/*
//Clear chap thua trong chapterk3_content
$db=new db;
$db->connect();
$db->query("select chap_id from chapterk3_content");
$dt=$db->fetchAll();
$j=0;
foreach($dt as $row){
	$db->query("select id from chapterk_content where chap_id=".$row['chap_id']);
	if($db->num_rows()>0){
	}else{
		echo $j++.":".$row['chap_id']." Thua <Br/>";
		$db->nonquery("delete from chapterk3_content where chap_id=".$row['chap_id']);
	//	echo mysql_error();
	}
}
$db->close();
*/
//Update set check_error = 1 where server = zing
/*
$db=new db;
$db->connect();
$db->query("select id,chap_id from chapterk_content where server='zing' and check_error=0 limit 0,7000");
$data=$db->fetchAll();
$j=0;
$i=0;
foreach($data as $row){
	$db->nonquery("update chapterk2 set server='zing',check_error=1,check_link=1 where id=".$row['chap_id']);
	$db->nonquery("update chapterk_content set  check_error=1,check_link=1 where chap_id=".$row['chap_id']);
		
	
}
*/
//Check chapter ton tai
/*
$db=new db;
$db->connect();
$db->query("select id,chap_id from chapterk22_content  limit 0,7000");
$data=$db->fetchAll();
$j=0;
$i=0;
foreach($data as $row){
	$db->query("select id,chap_id from chapterk_content where server='manga24h.com' and upload=0 and chap_id=".$row['chap_id']);
	
	if($db->num_rows()>0){
		echo "Chưa đổi link - Update được ".$row['chap_id']."<Br/>";
	}else{
		echo $i++;
		echo "Đổi Link rồi, del thôi ".$row['chap_id']."<Br/>";
		$db->nonquery("delete from chapterk333_content where chap_id=".$row['chap_id']);
	}
}
*/
//Check chapterk3_content-------------
/*
$db=new db;
$db->connect();
$db->query("select id,chap_id from chapterk3_content limit 0,5000");
$data=$db->fetchAll();
$j=0;
$i=0;
foreach($data as $row){
	$db->query("select id,chap_id from chapterk22_content where  chap_id=".$row['chap_id']);
	
	if($db->num_rows()>0){
		
		echo $j." : Co roi - Delete thoi".$row['chap_id']."<Br/>";
		$db->nonquery("delete from chapterk22_content where chap_id=".$row['chap_id']);
		$j++;
	}else{
		$i++;
		echo $i." : -Chua co ".$row['chap_id']."<Br/>";
	
	}
}
*/
// CHECK HINH ANH =========
/*
$db=new db;
$db->connect();
$db->query("select id,picasa,fix from chapterk22_content where fix=0  order by id asc limit 0,300");
$dt=$db->fetchAll();
foreach($dt as $picasas){
$picasa=unserialize($picasas['picasa']);
$links=array();
foreach($picasa as $link){
														$link=preg_replace('/https:\/\/lh3.googleusercontent.com/',"http://3.bp.blogspot.com",$link);
														$link=preg_replace('/https:\/\/lh4.googleusercontent.com/',"http://2.bp.blogspot.com",$link);
														$link=preg_replace('/https:\/\/lh5.googleusercontent.com/',"http://1.bp.blogspot.com",$link);
														$link=preg_replace('/https:\/\/lh6.googleusercontent.com/',"http://4.bp.blogspot.com",$link);
														$link = preg_replace('/\/s[0-9]*\//', '/s0/',$link);
														$links[]=$link;				
}
$links=serialize($links);
$sql="update chapterk22_content set picasa='".$links."',fix=1 where id=".$picasas['id'];
$db->nonquery($sql);
echo mysql_error();
echo "Xong chap".$picasas['id']."<Br/>";
//echo $sql." <Br/>";
}
*/
/*
$db=new db;
$db->connect();
$db->query("select id,chap_id,server from chapterk_content where fix=0 and server='upimgs' order by id desc limit 0,1000");
if($db->num_rows()>0){
	$dt=$db->fetchAll();
	foreach($dt as $row){
		$sql="update chapterk2 set server='upanh' where id=".$row['chap_id'];
		$db->nonquery($sql);
		$db->nonquery("update chapterk_content set fix=1 where id=".$row['id']);
		echo mysql_error();
		echo $sql."<Br/>";
	}
}
*/
/*
//Check link loi------------------
$db=new db;
$db->connect();
$db->nonquery("update anime set fix_update =0");
$db->query("select id from chapterk2  where check_link =1 and check_error=1  ");
echo mysql_error();
echo "Tong Link :".$db->num_rows()."<Br/>";
if($db->num_rows()>0){
	
	$sql="select id from anime where fix_update=0";
	$db->query($sql);
	if($db->num_rows()>0){
		$result=$db->fetchAll();
		foreach($result as $row){
			$db->query("select count(*) as num from chapterk2 where manga=".$row['id']." and check_link=1 and check_error=1");
			$re=$db->fetchOne();
			$sql="update anime set total_error=".$re['num'].", fix_update=1 where  id=".$row['id'];
			$db->nonquery($sql);
		}
	}
	
	
}
echo "Done";
*/
/*
$db=new db;
$db->connect();
//$db->nonquery("update anime set fix_update =0");
$db->query("select id from anime  where fix_update =0 order by id desc limit 0,100");
if($db->num_rows()>0){
	$dt=$db->fetchAll();
	foreach($dt as $row){
		$db->query("select count(*) as num from chapterk2 where manga=".$row['id']." and server='zing'");
		$re=$db->fetchOne();
		$sql="update anime set total_error=".$re['num'].", fix_update=1 where  id=".$row['id'];
		$db->nonquery($sql);
		echo mysql_error();
		echo $sql."<Br/>";
	}
}
*/
 /* ?>
$db=new db;
$db->connect();
$db->query("select * from anime where total_error >0 order by total_error asc");
if($db->num_rows()>0){
	$dt=$db->fetchAll();
	?>
    <table class="table table-bordered table-striped" style="background-color:#fff">
    <thead>
    	<tr>
        	<td>STT</td>
            <td>Manga</td>
            <td>Error</td>
      
        </tr>
    </thead>
    <tbody>
    
    <?php
	$j=1;
	foreach($dt as $row){
		?>
        <tr>
        	<td><?php echo $j++?></td>
            <td><?php echo short_str($row['name'],20);?></td>
            <td>
          
            <?php
			$db->query("select id,ten,seourl,fix,fix_user from chapterk2 where manga=".$row['id']." and check_link=1 and check_error=1 order by thutu asc");
			if($db->num_rows()>0){
				$data=$db->fetchAll();
				foreach($data as $rc){
				?>
            	<div>
                <?php if($rc['fix']==1){?> Fixed - <?php echo $rc['fix_user'];?> : <?php }?> 
                <a href="<?php echo url_anime_episode($rc['id'],$rc['seourl'],$row['alias']);?>" target="_blank"> <?php echo $row['name']." ".$rc['ten'];?></a></div>
                <?php
				}
			}
			 ?>	
            </td>
           
        </tr>
        <?php
	}
	?>
    </tbody>
    </table>
    <?php
}
*/
?>
			
 